﻿using System;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace NCMIS.ServiceModel.Ra
{
    /// <summary>
    /// Represents a Operation Behavior Attribute, that includes the XML Declaration in a service response of the called operation.
    /// Source: http://shevaspace.spaces.live.com/blog/cns!FD9A0F1F8DD06954!2012.entry
    /// </summary>
    public class IncludeXmlDeclarationAttribute : Attribute, IOperationBehavior
    {
        #region IOperationBehavior Members

        /// <summary>
        /// Passes data at runtime to bindings to support custom behavior. (Not used)
        /// </summary>
        /// <param name="operationDescription">The operation being examined. Use for examination only. If the operation
        /// description is modified, the results are undefined.</param>
        /// <param name="bindingParameters">The collection of objects that binding elements require to support the
        /// behavior.</param>
        public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
        {
            // Empty
        }

        /// <summary>
        /// Implements a modification or extension of the client across an operation. (Not used)
        /// </summary>
        /// <param name="operationDescription">The operation being examined. Use for examination only. If the operation
        /// description is modified, the results are undefined.</param>
        /// <param name="clientOperation">The run-time object that exposes customization properties for the operation
        /// described by operationDescription.</param>
        public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
        {
            // Empty
        }

        /// <summary>
        /// Changes the DispatchMessageFormatter of the service operation to the <see cref="NCMIS.ServiceModel.Ra.XmlDeclarationFormatter"/>.
        /// </summary>
        /// <param name="operationDescription">The operation being examined. Use for examination only. If the operation
        /// description is modified, the results are undefined.</param>
        /// <param name="dispatchOperation">The run-time object that exposes customization properties for the operation
        /// described by operationDescription.</param>
        public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
        {
            dispatchOperation.Formatter = new XmlDeclarationMessageFormatter(dispatchOperation.Formatter);
        }

        /// <summary>
        /// Confirms that the operation meets some intended criteria. (Not used)
        /// </summary>
        /// <param name="operationDescription">The operation being examined. Use for examination only. If the operation
        /// description is modified, the results are undefined.</param>
        public void Validate(OperationDescription operationDescription)
        {
            // Empty
        }

        #endregion

    }
}
